package com.wc.算法基础课.E第五讲动态规划.区间DP.石子合并;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/17 2:38
 * @description https://www.acwing.com/activity/content/punch_the_clock/11/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310;
    static int[][] f = new int[N][N];
    static int n;
    static int INF = 0x3f3f3f3f;
    static int[] s = new int[N];
    static int[] stones = new int[N];

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            stones[i] = sc.nextInt();
            s[i] = s[i - 1] + stones[i];
        }
        // 遍历区间合并长度
        for (int len = 2; len <= n; len++) {
            // 遍历起点, 终点不能超过ed
            for (int st = 1; st + len - 1 <= n; st++) {
                int ed = st + len - 1;
                // 初始化为最大值
                f[st][ed] = INF;
                // 遍历中点
                for (int md = st; md < ed; md++) {
                    f[st][ed] = Math.min(f[st][ed], f[st][md] + f[md + 1][ed] + s[ed] - s[st - 1]);
                }
            }
        }
        out.println(f[1][n]);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
